package com.ljb.main.v;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.VideoView;

import com.ljb.haikanglibrary.ControlModel;
import com.ljb.haikanglibrary.HaiKangManager;
import com.ljb.haikanglibrary.live.ConstantLive;
import com.ljb.haikanglibrary.live.LiveCallBack;
import com.ljb.haikanglibrary.utils.ThreadUtils;
import com.ljb.main.DataTemp;
import com.ljb.main.R;
import com.ljb.main.ui.LoadStateLayout;

/**
 * Created by ljb on 2017/5/26.
 */

public class BigLiveActivity extends BaseActivity {

    private VideoView vv_livefree;
    private ControlModel controlModel;
    private LoadStateLayout loadStateLayout;

    public static void actionStart(Context c) {
        c.startActivity(new Intent(c, BigLiveActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_livefree);
        initUI();
    }

    @Override
    protected void onResume() {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                controlModel = HaiKangManager.init().requestDeviceInfo(DataTemp.init().cameraInfo, new LiveCallBack() {
                    @Override
                    public void onMessageCallback(final int message) {
                        HaiKangManager.init().showMessage(message);
                        ThreadUtils.runUIThread(new Runnable() {
                            @Override
                            public void run() {
                                if (message == ConstantLive.PLAY_DISPLAY_SUCCESS) {
                                    loadStateLayout.setState(LoadStateLayout.STATE_SUCCESS);
                                } else if (message == ConstantLive.RTSP_FAIL) {
                                    loadStateLayout.setState(LoadStateLayout.STATE_ERROR);
                                } else if (message == ConstantLive.GET_PLAYURL_ERROR) {
                                    loadStateLayout.setState(LoadStateLayout.STATE_ERROR);
                                } else if (message == ConstantLive.START_OPEN_FAILED) {
                                    loadStateLayout.setState(LoadStateLayout.STATE_ERROR);
                                }
                            }
                        });
                    }
                });

                HaiKangManager.init().startLive(controlModel, 1, vv_livefree);
            }
        });
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (controlModel != null) {
            HaiKangManager.init().stopLive(controlModel);
        }
        super.onPause();
    }

    private void initUI() {
        loadStateLayout = (LoadStateLayout) findViewById(R.id.loadStateLayout);
        loadStateLayout.setErrorView(R.layout.layout_load_error);
        loadStateLayout.setLoadingView(R.layout.layout_loading);
        loadStateLayout.setState(LoadStateLayout.STATE_LOADING);
        vv_livefree = (VideoView) findViewById(R.id.vv_livefree);
    }
}
